package universum.engine;

import universum.util.ioUtility;

import java.security.PrivilegedAction;

public class UnsafeClassSecureLoader<T> {
    private PrivilegedAction<T> getUnsafeClassInitializer(final Class beingClass) {
        return new PrivilegedAction<T>() {
            public T run() {
                try {
                    return (T) beingClass.newInstance();
                } catch (Exception e) {
                    ioUtility.getInstance().writelnError(e);
                    return null;
                }
            }
        };
    }

    public T loadInSecureContext(Class unsafeClass) {
        return JungleSecurity.invokeInSecureContext(getUnsafeClassInitializer(unsafeClass));
    }

}
